<?php

// usrBase
class usrBase{    

    /*
        $data['sql1-inj'] = $db->get("SELECT title,click,catid FROM {docs_info} WHERE catid='%s' LIMIT 3", ["ad' or kid LIKE '%e%"]);
        static $whrtab = ['ckey', 'utype', 'umod', 'uname', 'pptuid'];
    */

    static function sqlWhere($whrs=[]){
        $whrstr = ''; // ['ckey', 'utype', 'umod', 'uname', 'pptuid']
        foreach($whrs as $wk=>$wv){
            $whrstr .= ($whrstr ? ' AND ' : '') . "$wk='$wv'";
        }
        return $whrstr;
    }

    // Kick out(踢出在线人员)
    static function setKickout($uname=''){
        $db = Dbop::db();
        return $db->run("DELETE FROM {active_login} WHERE uname='$uname'");
    }

    static function setLogout($nsp='', $whrs=[]){
        if(empty($whrs)){ $whrs['ckey'] = usrPerm::cktok($nsp); }
        $whrstr = self::sqlWhere($whrs);
        if(!$whrstr){ return 0; }
        usrPerm::ckdel($nsp);
        $db = Dbop::db();
        return $db->run("DELETE FROM {active_login} WHERE $whrstr");
    }
    static function keepLogin($nsp='', $whrs=[], $exp=600){
        if(empty($whrs)){ $whrs['ckey'] = usrPerm::cktok($nsp, 1); }
        $whrstr = self::sqlWhere($whrs);
        if(!$whrstr){ return 0; }
        $db = Dbop::db();
        $now = time(); $exp = $now - $exp - intval($exp/2); // 1.5倍
        return $db->run("UPDATE {active_login} SET atime='$now' WHERE $whrstr AND atime>='$exp'");
    }

    static function setLogin($nsp='', $urow=[]){
        if(empty($urow)){  return 0; }
        if(empty($urow['ckey'])){ $urow['ckey'] = usrPerm::cktok($nsp); }
        $skips = '[,atime,auser,aip,etime,euser,eip,]';
        $upds = '[,ckey,utype,umod,uname,pptuid,mname,mpic,]';
        $db = Dbop::db(); // ckey    utype   umod    uname   pptuid  mname   mpic    [mext]    atime   aip
        $fids = ''; $fval = ''; 
        $mext = ['_os'=>extEnv::userOS(),'_brs'=>extEnv::userBrowser(),'_time'=>date('m-d H:i')]; 
        foreach($urow as $wk=>$wv){
            $wv = str_replace(array('"',"'",'<','>'), array('`','`',"(",')'), $wv);
            if(strpos($skips,",$wk,")){ continue; }
            if(strpos($upds,",$wk,")){ 
                $fids .= "$wk,";
                $fval .= "'$wv',";
            }else{
                $mext[$wk] = $wv;
            }
        }
        $mext = json_encode($mext, JSON_UNESCAPED_UNICODE); $atime = time(); $aip = Basic::envIP();
        $sqldo = "REPLACE INTO {active_login} ({$fids}mext,atime,aip) VALUES ({$fval}'$mext','$atime','$aip')";
        return $db->run($sqldo);
    } 

    // re : Null, isLogin, Forbid, Info, array()
    static function getLogin($nsp='', $whrs=[]){
        if(empty($whrs)){ $whrs['ckey'] = usrPerm::cktok($nsp); }
        $whrstr = self::sqlWhere($whrs);
        if(!$whrstr){ return 0; }
        $db = Dbop::db();
        $urow = $db->get("SELECT * FROM {active_login} WHERE $whrstr", [], 1); 
        if(empty($urow)){
            $urow = ['utype'=>'(null)', 'umod'=>'person', 'uname'=>'(Guest)', 'mname'=>'(游客)', 'flg'=>'Guest'];
        }else{
            $urow['flg'] = 'Login';
        }
        return $urow;
    }
    
}
